package com.wms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.WarehousingEntryDto;
import com.wms.dto.WarehousingEntryItemDataDto;
import com.wms.mapper.ItemSkuMapper;
import com.wms.mapper.WarehousingEntryDetailsMapper;
import com.wms.mapper.WarehousingEntryMapper;
import com.wms.pojo.ItemSku;
import com.wms.pojo.PageResult;
import com.wms.pojo.WarehousingEntry;
import com.wms.pojo.WarehousingEntryDetails;
import com.wms.service.WarehousingEntryService;
import com.wms.vo.WarehousingEntryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class WarehousingEntryServiceImpl extends ServiceImpl<WarehousingEntryMapper, WarehousingEntry> implements WarehousingEntryService {

    @Autowired
    private WarehousingEntryDetailsMapper warehousingEntryDetailsMapper;
    @Autowired
    private ItemSkuMapper itemSkuMapper;

    @Override
    public PageResult queryPage(Integer page, Integer pageSize, String batchNumber) {
        PageHelper.startPage(page,pageSize);
        List<WarehousingEntryVo> list = getBaseMapper().queryPage(batchNumber);
        Page<WarehousingEntryVo> vos = (Page<WarehousingEntryVo>) list;
        return new PageResult(vos.getTotal(),vos.getResult());
    }

    @Override
    public WarehousingEntryDto getWarehousingEntryById(Integer id) {
        WarehousingEntry warehousingEntry = getBaseMapper().selectById(id);
        List<WarehousingEntryDetails> warehousingEntryDetails = warehousingEntryDetailsMapper.selectList(Wrappers
                .<WarehousingEntryDetails>lambdaQuery()
                .eq(WarehousingEntryDetails::getWarehousingEntryId, warehousingEntry.getId()));
        List<WarehousingEntryItemDataDto> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(warehousingEntryDetails)){
            warehousingEntryDetails.forEach(item->{
                Integer skuId = item.getSkuId();
                ItemSku itemSku = itemSkuMapper.selectById(skuId);
                // 添加空值检查，避免空指针异常
                String skuName = itemSku != null ? itemSku.getTitle() : "未知商品";
                String spuCode = itemSku != null ? itemSku.getCode() : "未知编码";
                WarehousingEntryItemDataDto warehousingEntryItemDataDto = WarehousingEntryItemDataDto.builder()
                        .price(item.getPrice())
                        .count(item.getCount())
                        .skuName(skuName)
                        .spuCode(spuCode)
                        .supplierId(item.getSupplierId())
                        .id(item.getId())
                        .build();
                list.add(warehousingEntryItemDataDto);
            });
        }
        WarehousingEntryDto warehousingEntryDto = WarehousingEntryDto.builder().id(warehousingEntry.getId())
                .batchNumber(warehousingEntry.getBatchNumber())
                .consignorId(warehousingEntry.getConsignorId())
                .predictedArrivalTime(warehousingEntry.getPredictedArrivalTime())
                .warehouseId(warehousingEntry.getWarehouseId())
                .detailsList(list).build();
        return warehousingEntryDto;
    }
}